<template>
  <!--面包屑导航-->
    <el-row style="margin: 5px 5px;">
        <el-col :span="24">
            <el-breadcrumb :separator-icon="ArrowRight">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>设备管理</el-breadcrumb-item>
                <el-breadcrumb-item>点检保养计划</el-breadcrumb-item>
            </el-breadcrumb>
        </el-col>
    </el-row>
  <!--查询条件区-->
    <el-row style="margin: 5px 5px;">
        <el-col :span="24">
            <el-form v-model="searchUpkeep" style="margin: 15px 0px 0px 0px" inline>
                <el-form-item style="width: 200px;" label="计划编码">
                    <el-input v-model="searchUpkeep.upkeepId"/>
                </el-form-item>
                <el-form-item style="width: 200px;" label="计划名称">
                    <el-input v-model="searchUpkeep.upkeepName"/>
                </el-form-item>
                <!--项目状态设置-->
                <el-form-item label="任务类型">
                    <el-select placeholder="全部" style="width: 100px;" v-model="searchUpkeep.upkeepType">
                        <el-option label="全部" :value="0"/>
                        <el-option label="设备点检" :value="1"/>
                        <el-option label="设备保养" :value="2"/>
                    </el-select>
                </el-form-item>
                <!--项目状态设置-->
                <el-form-item label="任务状态">
                    <el-select style="width: 100px;" v-model="searchUpkeep.upkeepStatus">
                           <template v-for="st in statusList">
                               <el-option :label="st.dictDataTag" :value="st.dictDataOrder"/>
                           </template>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button @click="search" type="primary">搜索</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button @click="cleanSearch">重置</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>

    <!--按钮功能区-->
    <el-row style="margin: 5px 5px;">
        <el-col :span="24">
            <el-button-group>
                <el-button @click="openInsert" type="primary">
                    新增
                </el-button>
                <el-button @click="loadUpdateInfo(chooseIds)"  :disabled="chooseIds.length != 1"  type="success">
                    修改
                </el-button>
                <el-button @click="deleteUpkeepList"   :disabled="chooseIds.length == 0" type="danger">
                    删除
                </el-button>
            </el-button-group>
        </el-col>
    </el-row>

    <!--表格主体-->
    <el-row style="margin: 5px 5px;">
        <el-col :span="24">
            <el-table
                :data="upkeepList"
                broder
                :stripe="true"
                row-key="upkeepId"
                size="small"
                @selection-change="changeChooses"
            >
                <el-table-column align="center" type="selection" width="55"/>
                <el-table-column align="center" label="计划编号">
                    <template #default="scope">
                        <el-link style="font-size: small" type="primary" @click="loadDesc(scope.row.upkeepId)">{{scope.row.upkeepId}}</el-link>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="upkeepName" label="计划名称"/>
                <el-table-column align="center" prop="upkeepType" label="计划类型">
                    <template #default="scope">
                        <template v-if="scope.row.upkeepType == 1">
                            设备点检计划
                        </template>
                        <template v-else>
                            设备保养计划
                        </template>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="upkeepStartTime" label="开始时间"/>
                <el-table-column align="center" prop="upkeepEndTime" label="结束时间"/>
                <el-table-column align="center" label="频率">
                    <template #default="scope">
                        {{scope.row.upkeepRate}} /
                        {{unitList[scope.row.upkeepRateUnit-1].dictDataTag}}
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="upkeepTypeStatus" label="计划状态">
                    <template #default="scope">
                        {{statusList[scope.row.upkeepStatus-1].dictDataTag}}
                    </template>
                </el-table-column>
                <el-table-column width="120" align="center" label="操作">
                    <template #default="scope">
                        <el-row>
                            <el-col :span="12">
                                <el-link @click="loadUpdateInfo(scope.row.upkeepId)" type="primary">
                                    <el-icon>
                                        <EditPen/>
                                    </el-icon>
                                    修改
                                </el-link>
                            </el-col>
                            <el-col :span="12">
                                <el-link @click="deleteUpkeep(scope.row)" type="primary">
                                    <el-icon>
                                        <DeleteFilled/>
                                    </el-icon>
                                    删除
                                </el-link>
                            </el-col>
                        </el-row>
                    </template>
                </el-table-column>
            </el-table>
        </el-col>
    </el-row>
    <!--分页-->
    <!--分页栏-->
    <el-row style="margin: 5px 5px;">
        <!-- 监听页码的变化事件 -->
        <el-pagination @change="changePage" :total="total" background layout="prev, pager, next"/>
    </el-row>

    <!--新增数据弹窗-->
    <el-dialog
        v-model="dialogUpkeepInsert"
        title="新增计划"
    >
        <el-form v-model="insertUpkeep" label-width="auto">
            <el-form-item label="计划名称">
                <el-input v-model="insertUpkeep.upkeepName"/>
            </el-form-item>
            <el-form-item label="计划类型">
                <el-select style="width: 220px;" v-model="insertUpkeep.upkeepType">
                    <el-option label="设备点检计划" value="1"/>
                    <el-option label="设备保养计划" value="2"/>
                </el-select>
            </el-form-item>
            <el-form-item label="计划评率">
                <el-input-number :min="1"  v-model="insertUpkeep.upkeepRate"/>
                <el-select placeholder="请选择频率" style="width: 125px" v-model="insertUpkeep.upkeepRateUnit">
                    <template v-for="unit in unitList">
                        <el-option :label="unit.dictDataTag" :value="unit.dictDataValue"/>
                    </template>
                </el-select>
            </el-form-item>
            <el-form-item label="开始时间">
                <el-date-picker
                    v-model="insertUpkeep.upkeepStartTime"
                    type="datetime"
                    placeholder="请选择开始时间"
                    format="YYYY-MM-DD HH:mm:ss"
                    value-format="YYYY-MM-DD HH:mm:ss"
                />
            </el-form-item>
            <el-form-item label="结束时间">
                <el-date-picker
                    v-model="insertUpkeep.upkeepEndTime"
                    type="datetime"
                    placeholder="请选择结束时间"
                    format="YYYY-MM-DD HH:mm:ss"
                    value-format="YYYY-MM-DD HH:mm:ss"
                />
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="insertUpkeep.upkeepNotes"/>
            </el-form-item>
            <el-form-item>
                <el-button @click="insertUp" type="primary">提交</el-button>
                <el-button @click="cancelInsertUpkeep">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <!--修改数据弹窗-->
    <el-dialog
        v-model="dialogUpkeepChange"
        title="修改计划"
    >
        <el-form v-model="updateUpkeep" label-width="auto">
            <el-form-item label="计划名称">
                <el-input v-model="updateUpkeep.upkeepName"/>
            </el-form-item>
            <el-form-item label="计划类型">
                <el-select style="width: 220px;" v-model="updateUpkeep.upkeepType">
                    <el-option label="设备点检计划" value="1"/>
                    <el-option label="设备保养计划" value="2"/>
                </el-select>
            </el-form-item>
            <el-form-item label="计划评率">
                <el-input-number :min="1"  v-model="updateUpkeep.upkeepRate"/>
                <el-select placeholder="请选择频率" style="width: 125px" v-model="updateUpkeep.upkeepRateUnit">
                    <template v-for="unit in unitList">
                        <el-option :label="unit.dictDataTag" :value="unit.dictDataValue"/>
                    </template>
                </el-select>
            </el-form-item>
            <el-form-item label="计划状态">
                <el-select v-model="updateUpkeep.upkeepStatus">
                    <template v-for="status in statusList">
                        <el-option :label="status.dictDataTag" :value="status.dictDataValue"/>
                    </template>
                </el-select>
            </el-form-item>
            <el-form-item label="开始时间">
                <el-date-picker
                    v-model="updateUpkeep.upkeepStartTime"
                    type="datetime"
                    placeholder="请选择开始时间"
                    format="YYYY-MM-DD HH:mm:ss"
                    value-format="YYYY-MM-DD HH:mm:ss"
                />
            </el-form-item>
            <el-form-item label="结束时间">
                <el-date-picker
                    v-model="updateUpkeep.upkeepEndTime"
                    type="datetime"
                    placeholder="请选择结束时间"
                    format="YYYY-MM-DD HH:mm:ss"
                    value-format="YYYY-MM-DD HH:mm:ss"
                />
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="updateUpkeep.upkeepNotes"/>
            </el-form-item>
            <el-tabs
                model-value="device"
                type="card"
                class="demo-tabs"
            >
                <el-tab-pane label="设备清单" name="device">
                    <el-button-group>
                        <el-button @click="insertDevice" type="primary" plain>新增</el-button>
                        <el-button @click="deleteDeviceList" :disabled="chooseDevice.length == 0" type="danger" plain>删除</el-button>
                    </el-button-group>
                    <el-table
                        :data="updateUpkeep.deviceList"
                        broder
                        :stripe="true"
                        row-key="deivceId"
                        size="small"
                        @selection-change="getUpdateDevice"
                    >
                        <el-table-column align="center" type="selection" width="55"/>
                        <el-table-column align="center" label="设备编号" prop="deviceId"/>
                        <el-table-column align="center" label="设备名称" prop="deviceName"/>
                        <el-table-column align="center" label="设备品牌" prop="deviceBrand"/>
                        <el-table-column align="center" label="设备信号" prop="deviceType"/>
                        <el-table-column align="center" label="操作">
                            <template #default="scope">
                                <el-link @click="deleteDevice(scope.row.deviceId)" type="primary">
                                    <el-icon>
                                        <DeleteFilled/>
                                    </el-icon>
                                    删除
                                </el-link>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
                <el-tab-pane label="计划清单" name="upkeepType">
                    <el-button-group>
                        <el-button @click="insertUpkeepType" type="primary" plain>新增</el-button>
                        <el-button @click="deleteUpkeepTypeList" :disabled="chooseUpkeepType.length == 0" type="danger" plain>删除</el-button>
                    </el-button-group>
                    <el-table
                        :data="updateUpkeep.upkeepTypeList"
                        broder
                        :stripe="true"
                        row-key="upkeepTypeId"
                        size="small"
                        @selection-change="getUpkeepUpkeepType"
                    >
                        <el-table-column align="center" type="selection" width="55"/>
                        <el-table-column align="center" width="65" label="项目编号" prop="upkeepTypeId"/>
                        <el-table-column align="center" width="90" label="项目名称" prop="upkeepTypeName"/>
                        <el-table-column align="center" label="项目内容" prop="upkeepTypeDesc"/>
                        <el-table-column align="center" label="项目标准" prop="upkeepTypeStandard"/>
                        <el-table-column align="center" label="操作">
                            <template #default="scope">
                                <el-link @click="deleteUpkeepTypeId(scope.row.upkeepTypeId)" type="primary">
                                    <el-icon>
                                        <DeleteFilled/>
                                    </el-icon>
                                    删除
                                </el-link>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
            </el-tabs>
            <el-form-item>
                <el-button @click="updateChange" type="primary">提交</el-button>
                <el-button @click="cancelUpdate">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <!--详情弹窗-->
    <el-dialog
        v-model="dialogUpkeepDesc"
        title="计划详情"
    >
        <el-descriptions>
            <el-descriptions-item label="计划编号">{{upkeepDesc.upkeepId}}</el-descriptions-item>
            <el-descriptions-item label="计划名称">{{upkeepDesc.upkeepName}}</el-descriptions-item>
            <el-descriptions-item label="计划类别">
                <template v-if="upkeepDesc.upkeepType == 1">
                    设备点检计划
                </template>
                <template v-else>
                    设备保养计划
                </template>
            </el-descriptions-item>
            <el-descriptions-item label="开始时间">{{upkeepDesc.upkeepStartTime}}</el-descriptions-item>
            <el-descriptions-item label="结束时间">{{upkeepDesc.upkeepEndTime}}</el-descriptions-item>
            <el-descriptions-item label="计划状态">
                <el-tag v-if="upkeepDesc.upkeepStatus == 1">
                    草稿
                </el-tag>
                <el-tag type="primary" v-else-if="upkeepDesc.upkeepStatus == 2">
                    待审核
                </el-tag>
                <el-tag type="success" v-else-if="upkeepDesc.upkeepStatus == 3">
                    已确认
                </el-tag>
                <el-tag type="warning" v-else-if="upkeepDesc.upkeepStatus == 4">
                    未通过
                </el-tag>
                <el-tag type="info" v-else>
                    已完成
                </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="计划评率">
                {{descRate}}
            </el-descriptions-item>
            <el-descriptions-item label="计划备注">
                {{upkeepDesc.upkeepNotes}}
            </el-descriptions-item>
        </el-descriptions>
        <el-tabs
            v-model="listChoose"
            type="card"
            class="demo-tabs"
        >
            <el-tab-pane label="设备清单" name="device">
                <el-table
                    :data="upkeepDesc.deviceList"
                    broder
                    :stripe="true"
                    row-key="deivceId"
                    size="small"
                >
                    <el-table-column align="center" label="设备编号" prop="deviceId"/>
                    <el-table-column align="center" label="设备名称" prop="deviceName"/>
                    <el-table-column align="center" label="设备品牌" prop="deviceBrand"/>
                    <el-table-column align="center" label="设备信号" prop="deviceType"/>
                </el-table>
            </el-tab-pane>
            <el-tab-pane label="计划清单" name="upkeepType">
                <el-table
                    :data="upkeepDesc.upkeepTypeList"
                    broder
                    :stripe="true"
                    row-key="deivceId"
                    size="small"
                >
                    <el-table-column align="center" width="65" label="项目编号" prop="upkeepTypeId"/>
                    <el-table-column align="center" width="90" label="项目名称" prop="upkeepTypeName"/>
                    <el-table-column align="center" label="项目内容" prop="upkeepTypeDesc"/>
                    <el-table-column align="center" label="项目标准" prop="upkeepTypeStandard"/>
                </el-table>
            </el-tab-pane>
        </el-tabs>
    </el-dialog>
    <!--新增设备弹窗-->
    <el-dialog
        v-model="dialogDevice"
        title="新增设备"
        width="1200"
    >
        <el-row >
            <el-col :span="4">
                <template v-for="typeOption in allTypeOptions">
                    <el-link @click="LimitDeviceType(typeOption.deviceTypeId)">
                        {{typeOption.deviceTypeName}}
                    </el-link>
                    <br>
                </template>
            </el-col>
            <el-col :span="20">
                <el-row>
                    <!--查询区-->
                    <el-col :span="24">
                        <el-form v-model="searchDevice" style="margin: 15px 0px 0px 0px" inline>
                            <el-form-item label="设备编号">
                                <el-input v-model="searchDevice.deviceId"/>
                            </el-form-item>
                            <el-form-item label="设备名称">
                                <el-input v-model="searchDevice.deviceName"/>
                            </el-form-item>
                            <el-form-item>
                                <el-button @click="loadDeviceList" type="primary">
                                    搜索
                                </el-button>
                                <el-button @click="cancelSearchDevice">
                                    重置
                                </el-button>
                            </el-form-item>
                        </el-form>
                    </el-col>
                </el-row>
                <!--表格主体-->
                <el-row style="margin: 5px 5px;">
                    <el-col :span="24">
                        <el-table
                            :data="deviceList"
                            broder
                            :stripe="true"
                            row-key="deviceId"
                            size="small"
                            @selection-change="getChooseDevice"
                        >
                            <el-table-column align="center" type="selection" width="55"/>
                            <el-table-column align="center" prop="deviceId" label="设备编号"/>
                            <el-table-column align="center" prop="deviceName" label="设备名称"/>
                            <el-table-column align="center" prop="deviceBrand" label="设备品牌"/>
                            <el-table-column align="center" prop="deviceType" label="设备型号"/>
                            <el-table-column align="center" prop="deviceWorkShopId" label="设备车间"/>
                            <el-table-column align="center" prop="deviceStatus" label="设备状态">
                                <template #default="scope">
                                    <el-tag type="primary" v-if="scope.row.deviceStatus == 1">正常</el-tag>
                                    <el-tag type="danger" v-else>停机</el-tag>
                                </template>
                            </el-table-column>
                            <el-table-column align="center" prop="deviceWorkShopId" label="设备车间"/>
                            <el-table-column align="center" prop="deviceCreateTime" label="创建时间"/>
                        </el-table>
                    </el-col>
                </el-row>
                <!--分页-->
                <!--分页栏-->
                <el-row style="margin: 5px 5px;">
                    <!-- 监听页码的变化事件 -->
                    <el-pagination @change="changeDevicePage" :total="totalDevice" background layout="prev, pager, next"/>
                </el-row>
            </el-col>
        </el-row>
        <el-row style="margin: 10px 5px;">
            <el-col :span="24">
                <el-button type="primary" @click="insertDeviceList">新增</el-button>
                <el-button @click="cancelInsertDevice">取消</el-button>
            </el-col>
        </el-row>
    </el-dialog>
    <!--新增项目弹窗弹窗-->
    <el-dialog
        v-model="dialogUpkeepType"
        title="新增项目"
        width="1200"
    >
        <!--查询条件区-->
        <el-row style="margin: 5px 5px;">
            <el-col :span="24">
                <el-form v-model="searchUpkeepType" style="margin: 15px 0px 0px 0px" inline>
                    <el-form-item label="项目编码">
                        <el-input v-model="searchUpkeepType.upkeepTypeId"/>
                    </el-form-item>
                    <el-form-item label="项目名称">
                        <el-input v-model="searchUpkeepType.upkeepTypeName"/>
                    </el-form-item>
                    <el-form-item>
                        <el-button @click="loadUpkeepTypeList" type="primary">搜索</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button @click="cleanSearchUpkeepType">重置</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
        <!--表格主体-->
        <el-row style="margin: 5px 5px;">
            <el-col :span="24">
                <el-table
                    :data="upkeepTypeList"
                    broder
                    :stripe="true"
                    row-key="upkeepTypeId"
                    size="small"
                    @selection-change="changeChoosesUpkeepType"
                >
                    <el-table-column align="center" type="selection" width="55"/>
                    <el-table-column align="center" prop="upkeepTypeId" label="项目编号">
                    </el-table-column>
                    <el-table-column align="center" prop="upkeepTypeName" label="项目名称"/>
                    <el-table-column align="center" prop="upkeepTypeType" label="项目类型">
                        <template #default="scope">
                            <template v-if="scope.row.upkeepTypeType == 1">
                                设备点检
                            </template>
                            <template v-else>
                                设备保养
                            </template>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="upkeepTypeDesc" label="项目内容"/>
                    <el-table-column align="center" prop="upkeepTypeStandard" label="项目标准"/>
                    <el-table-column align="center" prop="upkeepTypeNotes" label="项目备注"/>
                    <el-table-column align="center" prop="upkeepTypeStatus" label="是否启用">
                        <template #default="scope">
                            <el-tag type="primary" v-if="scope.row.upkeepTypeStatus == 1">是</el-tag>
                            <el-tag type="danger" v-else>否</el-tag>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
        <!--分页-->
        <!--分页栏-->
        <el-row style="margin: 5px 5px;">
            <!-- 监听页码的变化事件 -->
            <el-pagination @change="changePagekeepType" :total="totalUpkeepType" background layout="prev, pager, next"/>
        </el-row>
        <el-button-group>
            <el-button type="primary" @click="insertUpkeepTypeList">提交</el-button>
            <el-button @click="cancelInsertUpkeepType">取消</el-button>
        </el-button-group>
    </el-dialog>
</template>

<script setup>
import {ArrowRight} from "@element-plus/icons-vue";
import {onMounted, ref} from "vue";
import upkeep from "@/api/device/upkeep.js";
import dict from "@/api/dict.js";
import {ElMessage, ElMessageBox} from "element-plus";
import deviceType from "@/api/device/deviceType.js";
import device from "@/api/device/device.js";
import upkeepType from "@/api/device/upkeepType.js";

//封装新增项目弹窗
let dialogUpkeepType = ref(false)
//封装新增设备弹窗
let dialogDevice = ref(false)
//封装新增弹窗
let dialogUpkeepInsert = ref(false)
//封装修改弹窗
let dialogUpkeepChange = ref(false)
//封装详情窗口
let dialogUpkeepDesc = ref(false)
let upkeepList = ref([])
let searchUpkeep = ref({})
let insertUpkeep = ref({})
let updateUpkeep = ref({})
let listChoose = "device"
//封装数据总条数
let total = ref(0)
//封装页数信息
let pageNum = ref(1)
//封装数据总条数
let totalDevice = ref(0)
//封装页数信息
let pageNumDevice = ref(1)
//封装数据总条数
let totalUpkeepType = ref(0)
//封装页数信息
let pageNumUpkeepType = ref(1)
//封装状态信息
let statusList = ref([])
//封装单位信息
let unitList = ref([])
//封装选中的ids
let chooseIds = ref([])
//封装项目详情信息
let upkeepDesc = ref({})
//封装详情评率信息
let descRate = ref();
//封装device查询条件
let searchDevice = ref({})
//封装upkeepTpe查询条件
let searchUpkeepType = ref({})
//封装device信息
let deviceList = ref([])
//封装upkeepType信息
let upkeepTypeList = ref([])
//封装所有的选项
let allTypeOptions = ref([])
//封装要添加的设备Id
let chooseDeviceIds = ref([])
//封装传入数据
let upkeepConnectInsert = ref({})
//封装修改窗口选中的设备
let chooseDevice = ref([])
//封装要添加的点检项目Id
let chooseUpkeepTypeIds = ref([])
//封装修改页面选中的项目
let chooseUpkeepType = ref([])

function deleteUpkeepTypeList(){
    ElMessageBox.confirm(
        '你确定要删除设备吗',
        '删除项目',
        {
            confirmButtonText: '删除',
            cancelButtonText: '取消',
            type: 'error'
        }
    ).then(() => {
        chooseUpkeepType.value.forEach(upkeepTypeId => {
            console.log(upkeepTypeId)
            upkeep.deleteUPUPType(updateUpkeep.value.upkeepId,upkeepTypeId, res => {
                loadUpdateInfo(updateUpkeep.value.upkeepId)
            })
        })
        ElMessage({
            type: "success",
            message: "删除成功"
        })
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '已取消删除'
        })
    })
}

function getUpkeepUpkeepType(value){
    let tempIds = new Array();
    value.forEach(deviceV => {
        tempIds.push(deviceV.upkeepTypeId)
    })
    chooseUpkeepType.value = tempIds
}

function deleteUpkeepTypeId(upkeepTypeId){
    ElMessageBox.confirm(
        '你确定要删除设备吗',
        '删除项目',
        {
            confirmButtonText: '删除',
            cancelButtonText: '取消',
            type: 'error'
        }
    ).then(() => {
        upkeep.deleteUPUPType(updateUpkeep.value.upkeepId,upkeepTypeId, res => {
            if (1 == 1) {
                ElMessage({
                    type: "success",
                    message: "删除成功"
                })
            }
            loadUpdateInfo(updateUpkeep.value.upkeepId)
        })
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '已取消删除'
        })
    })
}

function cancelInsertUpkeepType(){
    chooseUpkeepTypeIds.value = new ref([])
    upkeepConnectInsert.value = new ref({})
    dialogUpkeepType.value = false

}
function insertUpkeepTypeList(){
    upkeepConnectInsert.value = new ref({})
    upkeepConnectInsert.value.upkeepId = updateUpkeep.value.upkeepId
    upkeepConnectInsert.value.upkeepTypeIds = chooseUpkeepTypeIds.value
    upkeep.insertUPUPType(upkeepConnectInsert.value,res=>{
        if (res.code == 200){
            ElMessage({
                type:"success",
                message:"添加成功"
            })
            loadUpdateInfo(updateUpkeep.value.upkeepId)
            cancelInsertUpkeepType()
        }else {
            ElMessage({
                type:"error",
                message:"添加失败"
            })
        }
    })
}

function changeChoosesUpkeepType(value){
    let tempIds = new Array();
    value.forEach(deviceV => {
        tempIds.push(deviceV.upkeepTypeId)
    })
    chooseUpkeepTypeIds.value = tempIds
}

function cleanSearchUpkeepType(){
    searchUpkeepType.value.upkeepTypeId = undefined
    searchUpkeepType.value.upkeepTypeName = undefined
    loadUpkeepTypeList()
}
function loadUpkeepTypeList(){
    upkeepType.loadAllUpkeepType(searchUpkeepType.value,pageNumUpkeepType.value,10,res=>{
        upkeepTypeList.value = res.data.list
        totalUpkeepType.value = res.data.total
    })
}
function insertUpkeepType(){
    dialogUpkeepType.value = true;
    //控制显示点检计划只显示点检项目，保养计划只显示包养项目
    searchUpkeepType.value.upkeepTypeType = updateUpkeep.value.upkeepType
    loadUpkeepTypeList()
}

function getUpdateDevice(value){
    let tempIds = new Array();
    value.forEach(deviceV => {
        tempIds.push(deviceV.deviceId)
    })
    chooseDevice.value = tempIds
}

function deleteDeviceList(){
    ElMessageBox.confirm(
        '你确定要删除设备吗',
        '删除项目',
        {
            confirmButtonText: '删除',
            cancelButtonText: '取消',
            type: 'error'
        }
    ).then(() => {
        let success = true;
        chooseDevice.value.forEach(deviceId => {
            upkeep.deleteUPDevice(updateUpkeep.value.upkeepId,deviceId, res => {
                loadUpdateInfo(updateUpkeep.value.upkeepId)
            })
        })
            ElMessage({
                type: "success",
                message: "删除成功"
            })
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '已取消删除'
        })
    })
}
function deleteDevice(deviceId){
    ElMessageBox.confirm(
        '你确定要删除设备吗',
        '删除项目',
        {
            confirmButtonText: '删除',
            cancelButtonText: '取消',
            type: 'error'
        }
    ).then(() => {
        upkeep.deleteUPDevice(updateUpkeep.value.upkeepId,deviceId, res => {
            if (1 == 1) {
                ElMessage({
                    type: "success",
                    message: "删除成功"
                })
            }
            loadUpdateInfo(updateUpkeep.value.upkeepId)
        })
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '已取消删除'
        })
    })
}
function cancelInsertDevice(){
    upkeepConnectInsert.value = new ref({})
    chooseDeviceIds.value = new ref([])
    dialogDevice.value = false;
}
function insertDevice(){
    dialogDevice.value = true
}
function insertDeviceList(){
    upkeepConnectInsert.value.upkeepId = updateUpkeep.value.upkeepId
    upkeepConnectInsert.value.deviceIds = chooseDeviceIds.value
    upkeep.insertUPDevice(upkeepConnectInsert.value,res=>{
        if (res.code == 200){
            ElMessage({
                type:"success",
                message:"添加成功"
            })
            loadUpdateInfo(updateUpkeep.value.upkeepId)
            cancelInsertDevice()
        }else {
            ElMessage({
                type:"error",
                message:"添加失败"
            })
        }
    })
}
function getChooseDevice(value){
    let tempIds = new Array();
    value.forEach(deviceV => {
        tempIds.push(deviceV.deviceId)
    })
    chooseDeviceIds.value = tempIds
}

function changeDevicePage(value){
    pageNumDevice.value = value
    loadDeviceList()
}
function cancelSearchDevice(){
    searchDevice.value.deviceName = undefined
    searchDevice.value.deviceId = undefined
    loadDeviceList()
}
function loadDeviceList(){
    device.loadAllDeviceList(searchDevice.value,pageNumDevice.value,10,res=>{
        deviceList.value = res.data.list
        totalDevice.value = res.data.total
    })
}
function LimitDeviceType(deviceTypeId){
    searchDevice.value.deviceTypeId = deviceTypeId;
    loadDeviceList()
}
function loadDesc(upkeepId){
    dialogUpkeepDesc.value = true;
    upkeep.loadUpkeepDetail(upkeepId,res=>{
        upkeepDesc.value = res.data
        descRate.value = res.data.upkeepRate + "/" + unitList.value[res.data.upkeepRateUnit - 1].dictDataTag
    })
}

function deleteUpkeepList(){
    chooseIds.value.forEach(id=>{
        upkeepList.value.forEach(upkeep=>{
            if (id == upkeep.upkeepId && upkeep.upkeepStatus != 1 && upkeep.upkeepStatus != 5){
                ElMessage({
                    type: "error",
                    message: "当前状态无法进行删除"
                })
                throw new Error("当前状态无法进行删除")
            }
        })
    })
    ElMessageBox.confirm(
        '你确定要删除保养计划吗',
        '删除项目',
        {
            confirmButtonText: '删除',
            cancelButtonText: '取消',
            type: 'error'
        }
    ).then(() => {
        let success = true;
        chooseIds.value.forEach(id => {
            upkeep.deleteUpkeep(id, res => {
                if (res.code != 1) {
                    success = false
                }
                loadUpkeepList()
            })
        })
        if (success) {
            ElMessage({
                type: "success",
                message: "删除成功"
            })
        } else {
            ElMessage({
                type: "error",
                message: "网络开了小差，请稍后重试"
            })
        }
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '已取消删除'
        })
    })
}


function changeChooses(value){
    let tempIds = new Array();
    value.forEach(upkeepType => {
        tempIds.push(upkeepType.upkeepId)
    })
    chooseIds.value = tempIds;
}

function updateChange(){
    upkeep.updateUpkeep(updateUpkeep.value,res=>{
        if (res.code == 1) {
            ElMessage({
                type: "success",
                message: "跟新成功"
            })
            loadUpkeepList()
            cancelUpdate()
        } else if (res.code == 300) {
            ElMessage({
                type: "warning",
                message: "计划名重复"
            })
        } else {
            ElMessage({
                type: "error",
                message: "添加失败"
            })
        }
    })
}

function insertUp(){
    if (insertUpkeep.value.upkeepName == undefined || insertUpkeep.value.upkeepRate == undefined || insertUpkeep.value.upkeepRateUnit == undefined || insertUpkeep.value.upkeepStartTime == undefined || insertUpkeep.value.upkeepEndTime == undefined){
        ElMessage({
            type: "error",
            message: "缺少关键元素"
        })
        throw new Error("缺少关键元素")
    }
    if (insertUpkeep.value.upkeepStartTime > insertUpkeep.value.upkeepEndTime){
        ElMessage({
            type: "error",
            message: "时间选择错误"
        })
        throw new Error("时间选择错误")
    }
    insertUpkeep.value.upkeepStatus = 1
    upkeep.insertUpkeep(insertUpkeep.value,res=>{
        if (res.code == 1) {
            ElMessage({
                type: "success",
                message: "添加成功"
            })
            loadUpkeepList()
            cancelInsertUpkeep()
        } else {
            ElMessage({
                type: "error",
                message: "添加失败"
            })
        }
    })
}

function cancelInsertUpkeep(){
    insertUpkeep.value = new ref({})
    dialogUpkeepInsert.value = false
}

function openInsert(){
    dialogUpkeepInsert.value = true
}


function cancelUpdate(){
    chooseDevice.value = new ref([])
    chooseUpkeepType.value = new ref([])
    updateUpkeep.value = new ref({})
    dialogUpkeepChange.value = false;
}

function loadUpdateInfo(upkeepId){
    upkeep.loadSearchInfo(upkeepId,res=>{
        updateUpkeep.value = res.data
    })
    dialogUpkeepChange.value = true
}


function deleteUpkeep(upkeepInfo){
    if (upkeepInfo.upkeepStatus != 1 && upkeepInfo.upkeepStatus != 5){
        ElMessage({
            type: "error",
            message: "当前状态无法进行删除"
        })
        throw new Error("当前状态无法进行删除")
    }
    ElMessageBox.confirm(
        '你确定要删除计划吗',
        '删除项目',
        {
            confirmButtonText: '删除',
            cancelButtonText: '取消',
            type: 'error'
        }
    ).then(() => {
        upkeep.deleteUpkeep(upkeepInfo.upkeepId, res => {
            if (res.code == 1) {
                ElMessage({
                    type: "success",
                    message: "删除成功"
                })
                loadUpkeepList()
            } else {
                ElMessage({
                    type: "error",
                    message: "网络开了小差，请稍后重试"
                })
            }
        })
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '已取消删除'
        })
    })
}

//改变页面数据
function changePage(value) {
    pageNum.value = value;
    loadUpkeepList()
}
function search(){
    loadUpkeepList()
}
function cleanSearch(){
    searchUpkeep.value = ref({})
    loadUpkeepList()
}
function loadUpkeepList(){
    upkeep.loadAllList(searchUpkeep.value,pageNum.value,10,res=>{
        upkeepList.value = res.data.list;
        total.value = res.data.total
    })
}
function loadDictData(dictId,callback){
    dict.loadDictDataList(dictId,res=>{
        callback(res.data)
    })
}
onMounted(()=>{
    loadUpkeepList()
    loadDictData(3,res=>{
        statusList.value = res
    })
    loadDictData(4,res=>{
        unitList.value = res
    })
    deviceType.loadAllOptions(res=>{
        allTypeOptions.value = res.data
    })
})
</script>

<style scoped>

</style>